package com.acme.bankapp.domain.bank;

import static com.acme.bankapp.service.bank.BankService.addClient;

import com.acme.bankapp.exceptions.bank.NotEnoughFundsException;
import com.acme.bankapp.exceptions.bank.OverDraftLimitExceededException;

public class CheckingAccount extends AbstractAccount{
	private int overdraft;
	
	public CheckingAccount(int initialValue, int overdraft) throws IllegalArgumentException{
		super(initialValue);
		if(overdraft < 0){
			throw new IllegalArgumentException("Negative overdraft");
		}
		this.overdraft = overdraft;
	}

	public void withdraw(int sum) throws OverDraftLimitExceededException{
		if(sum > this.getBalance()&& sum <= this.getBalance() + overdraft){
			overdraft = this.getBalance() + overdraft - sum;
			this.setBalance(0);
		}
		else if(sum <= this.getBalance()){
			setBalance(this.getBalance() - sum);
		}
		else{
			throw new OverDraftLimitExceededException(this);
		}

	}
	
	public int getOverdraft(){
		return overdraft;
	}
	
	public int maximumAmountToWithdraw(){
		return this.getBalance() + overdraft;
	}
	
	public static void main(String[] args) throws Exception{
		Bank bank = new Bank();		
		addClient(bank, new Client("Drozdov", Gender.MALE, 150,20));
		try{
		bank.getClients().get(0).getAccounts().get(0).withdraw(2000);
		}catch(OverDraftLimitExceededException e){
			e.getMessage();
			e.outputPrint();
		}
	}
}
